﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.UserMeetingRefs.Authorization;
using LeadThing.AbpZeroTemplate.UserMeetingRefs.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.UserMeetingRefs
{
    /// <summary>
    /// 视频会议服务实现
    /// </summary>
    [AbpAuthorize(UserMeetingRefAppPermissions.UserMeetingRef)]


    public class UserMeetingRefAppService : AbpZeroTemplateAppServiceBase, IUserMeetingRefAppService
    {
        private readonly IRepository<UserMeetingRef, long> _userMeetingRefRepository;
        private readonly IUserMeetingRefListExcelExporter _userMeetingRefListExcelExporter;


        private readonly UserMeetingRefManage _userMeetingRefManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public UserMeetingRefAppService(IRepository<UserMeetingRef, long> userMeetingRefRepository,
UserMeetingRefManage userMeetingRefManage
      , IUserMeetingRefListExcelExporter userMeetingRefListExcelExporter
  )
        {
            _userMeetingRefRepository = userMeetingRefRepository;
            _userMeetingRefManage = userMeetingRefManage;
            _userMeetingRefListExcelExporter = userMeetingRefListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<UserMeetingRef> _userMeetingRefRepositoryAsNoTrack => _userMeetingRefRepository.GetAll().AsNoTracking();


        #endregion


        #region 视频会议管理

        /// <summary>
        /// 根据查询条件获取视频会议分页列表
        /// </summary>
        public async Task<PagedResultDto<UserMeetingRefListDto>> GetPagedUserMeetingRefsAsync(GetUserMeetingRefInput input)
        {

            var query = _userMeetingRefRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var userMeetingRefCount = await query.CountAsync();

            var userMeetingRefs = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var userMeetingRefListDtos = userMeetingRefs.MapTo<List<UserMeetingRefListDto>>();
            return new PagedResultDto<UserMeetingRefListDto>(
            userMeetingRefCount,
            userMeetingRefListDtos
            );
        }

        /// <summary>
        /// 通过Id获取视频会议信息进行编辑或修改 
        /// </summary>
        public async Task<GetUserMeetingRefForEditOutput> GetUserMeetingRefForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetUserMeetingRefForEditOutput();

            UserMeetingRefEditDto userMeetingRefEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _userMeetingRefRepository.GetAsync(input.Id.Value);
                userMeetingRefEditDto = entity.MapTo<UserMeetingRefEditDto>();
            }
            else
            {
                userMeetingRefEditDto = new UserMeetingRefEditDto();
            }

            output.UserMeetingRef = userMeetingRefEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取视频会议ListDto信息
        /// </summary>
        public async Task<UserMeetingRefListDto> GetUserMeetingRefByIdAsync(EntityDto<long> input)
        {
            var entity = await _userMeetingRefRepository.GetAsync(input.Id);

            return entity.MapTo<UserMeetingRefListDto>();
        }







        /// <summary>
        /// 新增或更改视频会议
        /// </summary>
        public async Task CreateOrUpdateUserMeetingRefAsync(CreateOrUpdateUserMeetingRefInput input)
        {
            if (input.UserMeetingRefEditDto.Id.HasValue)
            {
                await UpdateUserMeetingRefAsync(input.UserMeetingRefEditDto);
            }
            else
            {
                await CreateUserMeetingRefAsync(input.UserMeetingRefEditDto);
            }
        }

        /// <summary>
        /// 新增视频会议
        /// </summary>
        [AbpAuthorize(UserMeetingRefAppPermissions.UserMeetingRef_CreateUserMeetingRef)]
        public virtual async Task<UserMeetingRefEditDto> CreateUserMeetingRefAsync(UserMeetingRefEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<UserMeetingRef>();

            entity = await _userMeetingRefRepository.InsertAsync(entity);
            return entity.MapTo<UserMeetingRefEditDto>();
        }

        /// <summary>
        /// 编辑视频会议
        /// </summary>
        [AbpAuthorize(UserMeetingRefAppPermissions.UserMeetingRef_EditUserMeetingRef)]
        public virtual async Task UpdateUserMeetingRefAsync(UserMeetingRefEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _userMeetingRefRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _userMeetingRefRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除视频会议
        /// </summary>
        [AbpAuthorize(UserMeetingRefAppPermissions.UserMeetingRef_DeleteUserMeetingRef)]
        public async Task DeleteUserMeetingRefAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _userMeetingRefRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除视频会议
        /// </summary>
        [AbpAuthorize(UserMeetingRefAppPermissions.UserMeetingRef_DeleteUserMeetingRef)]
        public async Task BatchDeleteUserMeetingRefAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _userMeetingRefRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 视频会议的Excel导出功能


        public async Task<FileDto> GetUserMeetingRefToExcel()
        {
            var entities = await _userMeetingRefRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<UserMeetingRefListDto>>();

            var fileDto = _userMeetingRefListExcelExporter.ExportUserMeetingRefToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
